package org.reborn.engine.render.util

import javax.media.opengl._
import _root_.javax.media.opengl.GL
import _root_.javax.media.opengl.GL._
import _root_.javax.media.opengl.GL2
import _root_.javax.media.opengl.GL2._
import _root_.javax.media.opengl.GL2ES2
import _root_.javax.media.opengl.GL2ES2._
import _root_.javax.media.opengl.fixedfunc.GLPointerFunc._

import _root_.org.reborn.engine.model.mesh.Mesh
import _root_.org.reborn.engine.model.mesh.DrawableMesh
import _root_.org.reborn.engine.model.mesh.VertexData
import _root_.org.reborn.engine.enums._
import ArrayType._
import FaceMode._


import java.nio.Buffer

object RenderMeshVertexArrayRange extends RenderMeshSupport{
  protected override val functionList = List(
    "glDrawElements",
    "glVertexPointer",
    "glNormalPointer",
    "glColorPointer",
    "glTexCoordPointer",
    "glClientActiveTexture",
    "glDrawRangeElements")
  
  def createRenderer:RenderMesh = new RenderMeshVertexArrayRange();
}

class RenderMeshVertexArrayRange extends RenderMeshVertexArray{
  
  override def render(gl:GL2,id:Int):Unit = {
    import gl._

    //Retrieve the mesh that was registered previously
    val mesh:DrawableMesh = meshRegistry.get(id).get

    //Determine what types of faces we are drawing (Points,Lines,Tris,Quads)
    val faceMode = getFaceMode(mesh)
    
    enableAvailableClientStates(gl,mesh)	//Enable arrays for any vertex data we have
    
    //Render the faces by using glDrawRangeElements
    val faceBuffer = mesh.getFacesBuffer;
    faceBuffer.position(0)
    glDrawRangeElements(faceMode, 0, mesh.verts,
                        mesh.faces * mesh.verticesPerFace, 
                        GL_UNSIGNED_INT, faceBuffer);
    
    disableEnabledClientStates(gl,mesh)		//Disable any enabled arrays
  }
    
}
